﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WindowsFormsApplication1.DataMGMT
{
   
    public class Valuable : IValuable
    {
        public ReaderWriterLockSlim m_ReaderWriterLockSlim;

        private Dictionary<int,IValue> m_Values;
        
        public bool Add(IValue p_Ivalue)
        {

            m_ReaderWriterLockSlim.EnterReadLock();
            if (m_Values.ContainsKey(p_Ivalue.GetType()))
            {
                m_ReaderWriterLockSlim.EnterReadLock();
                return false;
            }
            m_ReaderWriterLockSlim.EnterReadLock();

            m_ReaderWriterLockSlim.EnterWriteLock();
            m_Values.Add(p_Ivalue.GetType(), p_Ivalue);
            m_ReaderWriterLockSlim.ExitWriteLock();

            return true;
        }

        public IValue Get(int p_valueDef)
        {
            m_ReaderWriterLockSlim.EnterReadLock();

            if (m_Values.ContainsKey(p_valueDef))
            {
                return m_Values[p_valueDef];
            }

            return null;
           
        }
    }
}
